import { getCookie } from '@/utils/cookie'
import { showFailToast } from 'vant'
import { createRouter, createWebHashHistory } from 'vue-router'
const Login = () => import(/* webpackChunkName: 'loginRegister' */ '../views/LoginRegister/LoginView.vue')
const Register = () => import(/* webpackChunkName: 'loginRegister' */ '../views/LoginRegister/RegisterView.vue')
const Home = () => import(/* webpackChunkName: 'home' */ '../views/Home/IndexView.vue')
const Detail = () => import(/* webpackChunkName: 'detail' */ '../views/Detail/IndexView.vue')
const Category = () => import(/* webpackChunkName: 'category' */ '../views/Category/IndexView.vue')
const Mine = () => import(/* webpackChunkName: 'mine' */ '../views/Mine/IndexView.vue')
const Collect = () => import(/* webpackChunkName: 'collect' */ '../views/Mine/component/CollectView.vue')
const Address = () => import(/* webpackChunkName: 'address' */ '../views/Mine/component/AddressView.vue')
const EditAddress = () => import(/* webpackChunkName: 'address' */ '../views/Mine/component/EditAddressView.vue')
const Shopcart = () => import(/* webpackChunkName: 'shopcart' */ '../views/Shopcart/IndexView.vue')

const routes = [
  { path: '/login', name: 'login', component: Login },
  { path: '/register', name: 'register', component: Register },
  { path: '/home', name: 'home', component: Home },
  { path: '/detail', name: 'detail', component: Detail },
  { path: '/category', name: 'category', component: Category },
  { path: '/mine', name: 'mine', component: Mine, meta: { isShouQuan: true } },
  { path: '/collect', name: 'collect', component: Collect, meta: { isShouQuan: true } },
  { path: '/address', name: 'address', component: Address, meta: { isShouQuan: true } },
  { path: '/editAddress', name: 'editAddress', component: EditAddress, meta: { isShouQuan: true } },
  { path: '/shopcart', name: 'shopcart', component: Shopcart, meta: { isShouQuan: true } }
  // { path: '/:pathMatch(.*)*', redirect: '/home' }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to: any, from: any, next: any) => {
  if (to.meta.isShouQuan && ((getCookie('token') === undefined || getCookie('token') === '' || getCookie('token') === null))) {
    showFailToast({ message: '请先登录哦', duration: 1500, forbidClick: true })
    return next({ path: '/login', query: { backTo: to.fullPath } })
  } else {
    next()
  }
})

export default router
